//
//  InfoDetail.m
//  Trading365
//
//  Created by Jason on 12-2-3.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "QuotationInfo2.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "QuotProductListCell.h"


@implementation QuotationInfo2
@synthesize table;
@synthesize InfoListTitle,InfoListName;
@synthesize responseParams,total_usd;
@synthesize quotationNO2;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title=self.key_name;
    self.table.backgroundColor=BACKGROUND_COLOR;

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListTitle = title_array; 
    [title_array release];
   
    NSArray *name_title = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListName = name_title; 
    [name_title release];
    
    total_usd = [[NSMutableArray alloc] initWithCapacity:0];
    
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [[[TaskManager Share] GetTaskByType:GetQuotationInfo2 Delegate:self] cancel];
}

-(void)viewWillAppear:(BOOL)animated
{
    [total_usd removeAllObjects];
}

- (void)dealloc {
    [responseParams release];
    [total_usd release];
    [InfoListTitle release];
    [InfoListName release];
    [quotationNO2 release];
    [table release];
    [super dealloc];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
        return 44;
    else
        return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0)
        return 1;
    else
        return [self.responseParams count];    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0){
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        
        // Configure the cell...
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        
        cell.textLabel.text = @"美元总金额";
        cell.detailTextLabel.text=@"0";
        float totale=0.0f;
        for (int i=0; i<[total_usd count]; i++)
        {
            float a=[[total_usd objectAtIndex:i] floatValue];
            totale = totale + a;
        }
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.4f",totale];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;
    }

    static NSString *CellIdentifier = @"QuotProductListCell";
        
    QuotProductListCell* cell = (QuotProductListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotProductListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    NSUInteger row = [indexPath row];
    
    if (row >= [responseParams count])
    {
        return cell;
    }
    
    NSMutableDictionary* dic = [responseParams objectAtIndex:row];    
    cell.name.text =[dic objectForKey:@"prod_name_cn"];

    //美元金额＝原币单价＊对美元汇率＊统计数量
    //原币单价
    float ybdj=[[dic objectForKey:@"extrade_unit_price_orgcny"] floatValue];
    cell.value3.text =[NSString stringWithFormat:@"%.2f",ybdj];

    //对美元汇率
    float rate2usd=[[dic objectForKey:@"rate2usd"] floatValue];
    
    //统计数量
    int tjsl=[[dic objectForKey:@"basic_quantity"] intValue];
    NSString *tmp2=[NSString stringWithFormat:@"%i(%@)",tjsl,[dic objectForKey:@"basic_unit"]];
    cell.value2.text =tmp2;
    
    //美元金额
    float tot_usd=ybdj*rate2usd*tjsl;
    
    cell.value1.text =[NSString stringWithFormat:@"%.2f",tot_usd];;
//    [total_usd addObject:[NSString stringWithFormat:@"%.4f",tot_usd]];
//[total_usd setValue:[dic objectForKey:@"prod_usd"] forKey:[NSString stringWithFormat:@"%d",row]];
                
    return cell;

}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    
//    ProductInfoDetail *info = [[ProductInfoDetail alloc] init];
//    [info setInfoDetail:responseParam];
//    [self.navigationController pushViewController:info animated:YES];
//    [info release];
     
}



#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) DoInBackground
{
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableArray *tmp;

    tmp = [xml GetQuotationProductList:self.quotationNO2 startRows:@"0" rowsPerPage:@"100"];
    
    responseParams = [[NSMutableArray alloc] initWithArray:tmp];
 
    if ([responseParams count] > 0){
        
        for (NSMutableDictionary* dic in responseParams){
            //美元金额＝原币单价＊对美元汇率＊统计数量
            //原币单价
            float ybdj=[[dic objectForKey:@"extrade_unit_price_orgcny"] floatValue];
            
            //对美元汇率
            float rate2usd=[[dic objectForKey:@"rate2usd"] floatValue];
            
            //统计数量
            int tjsl=[[dic objectForKey:@"basic_quantity"] intValue];
            
            //美元金额
            float tot_usd=ybdj*rate2usd*tjsl;

            [total_usd addObject:[NSString stringWithFormat:@"%.4f",tot_usd]];
        }
    }
    
    [xml release];
    [self.table reloadData];
}

-(void) OnPostExcute
{
    [super OnPostExcute];
}

-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetQuotationInfo2 Delegate:self] Excute];
}

@end
